﻿using System;
using System.Data;
using Besture.Bll;
using Besture.Common;
using Besture.Model;
using Besture.UI.Admin.Master;

namespace Besture.UI.Admin.LabelManage
{
    public partial class LabelSet : BasePage
    {
        public string _ArticleType = "0";

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {

            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
            ((ConfigAdmin)this.Master).Title = "数据源管理";
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
            //加载所有表数据
            DataTable table =
                QueryDac.GetTable("select name from sys.all_objects where name like 'tb%' order by NAME asc");

            DataRow row = table.NewRow();
            row["name"] = "请选择";
            table.Rows.InsertAt(row,0);

            ddlTable1.DataSource = table;
            ddlTable1.DataTextField = "name";
            ddlTable1.DataValueField = "name";
            ddlTable1.DataBind();

            ddlTable2.DataSource = table;
            ddlTable2.DataTextField = "name";
            ddlTable2.DataValueField = "name";
            ddlTable2.DataBind();
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if(ID<=0)
            {
                return;
            }
            var sqlDac = new SqlDac();
            var model = sqlDac.GetModel(ID);

            Name.Text = model.Name;
            txtSql.Text = model.SqlText;
            txtRemark.Text = model.Remarks;

        }
        #endregion

        #region 保存

        /// <summary>
        /// 添加数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
             SqlDac dac = new SqlDac();
            SqlModel model;
            //添加
            if (!EditPage)
            {
                model = new SqlModel();
                model.Name = Name.Text;
                model.SqlText = txtSql.Text;
                model.Remarks = txtRemark.Text;
               
                dac.Add(model);
            }
            else
            {
                model = dac.GetModel(ID);
                model.Name = Name.Text;
                model.SqlText = txtSql.Text;
                model.Remarks = txtRemark.Text;
                dac.Update(model);
            }

            RedirectUrl("LabelList.aspx","保存成功");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("ArticleList.aspx");
        }

        #endregion
    }
}